﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DSAManager.Core;
using DSAManager.Models.Input;
using DSAManager.Utils;

namespace DSAManager
{
    public partial class FrmAdd : Form
    {
        public FrmAdd()
        {
            InitializeComponent();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            TaobaoOrderInfo order = new TaobaoOrderInfo();
            order.no = txt_orderno.Text;
            order.buyernick = txt_wangwang.Text;
            order.Source = radioButton1.Checked ? "0" : "1";

            order.receivermobile = txt_mobile.Text;

            if (string.IsNullOrEmpty(order.no))
            {
                MsgBox.Alert("订单号不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(order.buyernick))
            {
                MsgBox.Alert("旺旺不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(order.receivermobile))
            {
                MsgBox.Alert("手机号码不能为空!");
                return;
            }
            order.orders = new List<TaobaoSunOrderInfo>();
            int num = 0;
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                TaobaoSunOrderInfo sunOrder = new TaobaoSunOrderInfo();
                sunOrder.oid = IdWorker.Instance.nextId().ToString();
                sunOrder.no = order.no;
                sunOrder.num = dataGridView1[4, i].Value == null ? "" : dataGridView1[4, i].Value.ToString();
                sunOrder.producttype = dataGridView1[0, i].Value == null ? "" : dataGridView1[0, i].Value.ToString();
                sunOrder.skupropertiesname = dataGridView1[1, i].Value == null ? "" : dataGridView1[1, i].Value.ToString();
                sunOrder.picpaper = dataGridView1[2, i].Value == null ? "" : dataGridView1[2, i].Value.ToString();
                sunOrder.packagetype = dataGridView1[3, i].Value == null ? "" : dataGridView1[3, i].Value.ToString();
                sunOrder.title = dataGridView1[5, i].Value == null ? "" : dataGridView1[5, i].Value.ToString();
                sunOrder.Source = radioButton1.Checked ? "0" : "1";


                if (string.IsNullOrEmpty(sunOrder.producttype))
                {
                    MsgBox.Alert(string.Format("第{0}行,照片模式不能为空!", i + 1));
                    return;
                }

                if (string.IsNullOrEmpty(sunOrder.skupropertiesname))
                {
                    MsgBox.Alert(string.Format("第{0}行,照片尺寸不能为空!", i + 1));
                    return;
                }

                if (string.IsNullOrEmpty(sunOrder.picpaper))
                {
                    MsgBox.Alert(string.Format("第{0}行,相纸不能为空!", i + 1));
                    return;
                }

                if (string.IsNullOrEmpty(sunOrder.packagetype))
                {
                    MsgBox.Alert(string.Format("第{0}行,包装方式不能为空!", i + 1));
                    return;
                }

                if (IntExtension.Parse(sunOrder.num) <= 0)
                {
                    MsgBox.Alert(string.Format("第{0}行,数量格式不正确!", i + 1));
                    return;
                }

                if (string.IsNullOrEmpty(sunOrder.title))
                {
                    MsgBox.Alert(string.Format("第{0}行,产品名称不能为空!", i + 1));
                    return;
                }

                num += IntExtension.Parse(sunOrder.num);
                order.orders.Add(sunOrder);

            }
            order.num = num.ToString();
            order.created = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            if (order.orders.Count == 0)
            {
                MsgBox.Alert("请至少添加一规格!");
                return;
            }

            var result = Api.AddOrder(order);
            if (result.ok)
            {
                MsgBox.Alert("添加订单成功!");
            }
            else
            {
                MsgBox.Alert(result.msg);
            }
        }
    }
}
